/**
 *  iBizSys 5.0 机器人生产代码（不要直接修改当前代码）
 *  http://www.ibizsys.net
 */
package com.sa.ibiz.demo.demoapp.srv.sys.ctrlmodel;


import java.util.ArrayList;
import net.ibizsys.paas.util.StringHelper;
import net.ibizsys.paas.web.AjaxActionResult;
import net.ibizsys.paas.web.WebContext;
import net.ibizsys.paas.web.WebContext;
import net.ibizsys.paas.data.IDataObject;

import net.ibizsys.paas.web.WebConfig;
import net.ibizsys.paas.control.menu.AppMenuItem;
import net.ibizsys.paas.control.menu.AppMenuRootItem;
import net.ibizsys.paas.control.menu.AppMenuUtils;
import net.ibizsys.paas.ctrlmodel.AppMenuModelGlobal;
import java.io.File;
import javax.annotation.PostConstruct;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
/**
 * 应用菜单[Demo]模型基类
 */
@Component
public class  DemoAppDemoAppMenuModel extends net.ibizsys.paas.ctrlmodel.AppMenuModelBase {
    private Log log = LogFactory.getLog(DemoAppDemoAppMenuModel.class);

    public DemoAppDemoAppMenuModel()  {
        super();
        this.setName("Demo");
    }

    @Override
    protected void onInit() throws Exception {
        super.onInit();
    }

    /**
     * 注解Spring构造后执行
     * @throws Exception
     */
    @PostConstruct
    public void postConstruct() throws Exception {
        AppMenuModelGlobal.registerAppMenuModel("com.sa.ibiz.demo.demoapp.srv.sys.ctrlmodel.DemoAppDemoAppMenuModel",this);
        AppMenuModelGlobal.registerAppMenuModel("E80E0D31-0F7D-441B-A97B-A28BB1AEB25C",this);
    }

    /**
     * 准备应用菜单根节点
     * @param appMenuRootItem
     * @throws Exception
     */
    @Override
    protected void onPrepareRootItem(AppMenuRootItem appMenuRootItem) throws Exception {
        //从Xml文件加载菜单项
        boolean bSuccessLoaded = this.loadAppMenuRootItem(appMenuRootItem);
        if(bSuccessLoaded)
            appMenuRootItem = new AppMenuRootItem();

        //添加 工作流角色
        AppMenuItem appMenuItem0 = appMenuRootItem.addItem("B593D661-FCC0-4D6B-8D1E-0F2ECC4D56B4","");
        appMenuItem0.setAppFuncId("F9407D44-A488-48C3-94F3-B2CD30E3D66D");
        appMenuItem0.setItemType("MENUITEM");
        appMenuItem0.setText("工作流角色");
        appMenuItem0.setTextLanResTag("");
        appMenuItem0.setTooltip("工作流角色");
        appMenuItem0.setTooltipLanResTag("");
        appMenuItem0.setAccUserMode(2);
        //添加 系统管理
        AppMenuItem appMenuItem1 = appMenuRootItem.addItem("72685B70-906C-4317-B849-067028C9CFFB","");
        appMenuItem1.setAppFuncId("");
        appMenuItem1.setItemType("MENUITEM");
        appMenuItem1.setText("系统管理");
        appMenuItem1.setTextLanResTag("");
        appMenuItem1.setTooltip("系统管理");
        appMenuItem1.setTooltipLanResTag("");
        //添加 组织
        AppMenuItem appMenuItem2 = appMenuRootItem.addItem("9E24C7AC-B78F-4599-8980-4E8F6C8938FC","72685B70-906C-4317-B849-067028C9CFFB");
        appMenuItem2.setAppFuncId("64C33C81-4A04-4F1A-8E2C-B97E6A2A6D63");
        appMenuItem2.setItemType("MENUITEM");
        appMenuItem2.setText("组织");
        appMenuItem2.setTextLanResTag("");
        appMenuItem2.setTooltip("组织");
        appMenuItem2.setTooltipLanResTag("");
        appMenuItem2.setAccUserMode(2);
        //添加 部门
        AppMenuItem appMenuItem3 = appMenuRootItem.addItem("5344FB88-E203-49AC-9288-036AFB928500","72685B70-906C-4317-B849-067028C9CFFB");
        appMenuItem3.setAppFuncId("F0396FEF-AFBE-46E6-866B-159FEE0261EC");
        appMenuItem3.setItemType("MENUITEM");
        appMenuItem3.setText("部门");
        appMenuItem3.setTextLanResTag("");
        appMenuItem3.setTooltip("部门");
        appMenuItem3.setTooltipLanResTag("");
        appMenuItem3.setAccUserMode(2);
        //添加 人员
        AppMenuItem appMenuItem4 = appMenuRootItem.addItem("5E45F55E-156C-478B-B853-A7C484CE56EF","72685B70-906C-4317-B849-067028C9CFFB");
        appMenuItem4.setAppFuncId("C263A049-A5DD-4399-9C16-4D6B1EE1F3C3");
        appMenuItem4.setItemType("MENUITEM");
        appMenuItem4.setText("人员");
        appMenuItem4.setTextLanResTag("");
        appMenuItem4.setTooltip("人员");
        appMenuItem4.setTooltipLanResTag("");
        appMenuItem4.setAccUserMode(2);
        //添加 登录账号
        AppMenuItem appMenuItem5 = appMenuRootItem.addItem("1A843A6B-6B75-42F9-9B92-FE32E4E03664","72685B70-906C-4317-B849-067028C9CFFB");
        appMenuItem5.setAppFuncId("33A98F22-4887-4B4C-8D93-B89F6B40B538");
        appMenuItem5.setItemType("MENUITEM");
        appMenuItem5.setText("登录账号");
        appMenuItem5.setTextLanResTag("");
        appMenuItem5.setTooltip("登录账号");
        appMenuItem5.setTooltipLanResTag("");
        appMenuItem5.setAccUserMode(2);
        //添加 实体
        AppMenuItem appMenuItem6 = appMenuRootItem.addItem("200AD8A0-13B9-4FE8-B754-012EDB983A5D","72685B70-906C-4317-B849-067028C9CFFB");
        appMenuItem6.setAppFuncId("20718C31-B35C-4A4B-8CEC-47142CE442D0");
        appMenuItem6.setItemType("MENUITEM");
        appMenuItem6.setText("实体");
        appMenuItem6.setTextLanResTag("");
        appMenuItem6.setTooltip("实体");
        appMenuItem6.setTooltipLanResTag("");
        appMenuItem6.setAccUserMode(2);
        //添加 数据对象能力
        AppMenuItem appMenuItem7 = appMenuRootItem.addItem("D7A2322C-9922-4F87-9E0C-F8CC205689BE","72685B70-906C-4317-B849-067028C9CFFB");
        appMenuItem7.setAppFuncId("DEB6D12C-470A-41C8-90FD-9675998DD822");
        appMenuItem7.setItemType("MENUITEM");
        appMenuItem7.setText("数据对象能力");
        appMenuItem7.setTextLanResTag("");
        appMenuItem7.setTooltip("数据对象能力");
        appMenuItem7.setTooltipLanResTag("");
        appMenuItem7.setAccUserMode(2);
        //添加 用户角色
        AppMenuItem appMenuItem8 = appMenuRootItem.addItem("AFF4E21F-2A9A-4C15-936F-D596444612CF","72685B70-906C-4317-B849-067028C9CFFB");
        appMenuItem8.setAppFuncId("704E52AD-7872-4EEA-9CB1-9B7EEBDEE262");
        appMenuItem8.setItemType("MENUITEM");
        appMenuItem8.setText("用户角色");
        appMenuItem8.setTextLanResTag("");
        appMenuItem8.setTooltip("用户角色");
        appMenuItem8.setTooltipLanResTag("");
        appMenuItem8.setAccUserMode(2);

        if(WebConfig.getCurrent().getAttribute("EXPORTMENUCONF", false)) {
            //菜单项写入Xml文件
            this.writeAppMenuRootItem(appMenuRootItem);
        }
    }

    /**
     * 从菜单配置文件加载菜单项
     * @return
     */
    protected boolean loadAppMenuRootItem(AppMenuRootItem appMenuRootItem) {
        try {
            String strMenuConfigFileName = this.getMenuConfigFileName(false);
            File file = new File(strMenuConfigFileName);
            if(!file.exists())
                return false;

            AppMenuRootItem appMenuRootItem2 = AppMenuUtils.loadXmlFile(appMenuRootItem, strMenuConfigFileName);
            if(appMenuRootItem2 == null)
                return false;

            return true;
        } catch (Exception ex) {
            log.error(StringHelper.format("加载菜单配置数据发生错误！错误信息：%1$s", ex.getMessage()));
            ex.printStackTrace();
            return false;
        }
    }

    /**
     * 写菜单数据配置文件
     * @param appMenuRootItem
     */
    protected void writeAppMenuRootItem(AppMenuRootItem appMenuRootItem) {
        try {
            String strOriginMenuConfigFileName = this.getMenuConfigFileName(true);
            File file = new File(strOriginMenuConfigFileName);
            if(!file.exists()) {
                File parentFile = new File(file.getParent());
                if(!parentFile.exists())
                    parentFile.mkdirs();
            }

            //写菜单配置数据原始文件
            AppMenuUtils.writeXmlFile(appMenuRootItem, strOriginMenuConfigFileName);
        } catch (Exception ex) {
            log.error(StringHelper.format("写菜单配置数据发生错误！错误信息：%1$s", ex.getMessage()));
            ex.printStackTrace();
        }
    }

    /**
     * 获取菜单配置数据Xml文件路径
     * @return
     */
    protected String getMenuConfigFileName(Boolean bOriginName) {
        File file = new File(this.getClass().getResource("/").getPath());
        String strFileName = (this.getClass().getName()).replaceAll("\\.", "_");
        return StringHelper.format("%1$s%2$smenuconf%2$s%3$s%4$s.xml",file.getPath(), File.separator, bOriginName?"Origin_":"", strFileName);
    }

}